import {ExecutionContext, Injectable, NestInterceptor} from '@nestjs/common'
import {Observable} from "rxjs";
import {tap} from 'rxjs/operators';

@Injectable()
export class LoggerInterceptor implements NestInterceptor {
    intercept(context: ExecutionContext, next: any): Observable<any> | Promise<Observable<any>> {
        console.log('LoggerInterceptor Before...');

        const now = Date.now();
        // next.handle 就是原方法
        return next.handle()
            .pipe(
                // tap在 handle执行后执行，传入一个函数该函数接收参数就是 原方法的返回值
                tap((handleValue) => {
                    console.log(`LoggerInterceptor After... ${Date.now() - now}ms`)
                })
            )
    }
}

@Injectable()
export class LoggerInterceptor2 implements NestInterceptor {
    async intercept(context: ExecutionContext, next: any): Promise<Observable<any>> {
        console.log('LoggerInterceptor2 Before...');

        const now = Date.now();
        // next.handle 就是原方法
        return next.handle()
            .pipe(
                // tap在 handle执行后执行，传入一个函数该函数接收参数就是 原方法的返回值
                tap((handleValue) => {
                    console.log(`LoggerInterceptor2 After... ${Date.now() - now}ms`)
                })
            )
    }
}

@Injectable()
export class LoggerInterceptor3 implements NestInterceptor {
    async intercept(context: ExecutionContext, next: any): Promise<Observable<any>> {
        console.log('LoggerInterceptor3 Before...');

        const now = Date.now();
        // next.handle 就是原方法
        return next.handle()
            .pipe(
                // tap在 handle执行后执行，传入一个函数该函数接收参数就是 原方法的返回值
                tap((handleValue) => {
                    console.log(`LoggerInterceptor3 After... ${Date.now() - now}ms`)
                })
            )
    }
}

@Injectable()
export class LoggerInterceptor4 implements NestInterceptor {
    async intercept(context: ExecutionContext, next: any): Promise<Observable<any>> {
        console.log('LoggerInterceptor4 Before...');

        const now = Date.now();
        // next.handle 就是原方法
        return next.handle()
            .pipe(
                // tap在 handle执行后执行，传入一个函数该函数接收参数就是 原方法的返回值
                tap((handleValue) => {
                    console.log(`LoggerInterceptor4 After... ${Date.now() - now}ms`)
                })
            )
    }
}


@Injectable()
export class LoggerInterceptor5 implements NestInterceptor {
    async intercept(context: ExecutionContext, next: any): Promise<Observable<any>> {
        console.log('LoggerInterceptor5 Before...');

        const now = Date.now();
        // next.handle 就是原方法
        return next.handle()
            .pipe(
                // tap在 handle执行后执行，传入一个函数该函数接收参数就是 原方法的返回值
                tap((handleValue) => {
                    console.log(`LoggerInterceptor5 After... ${Date.now() - now}ms`)
                })
            )
    }
}


@Injectable()
export class LoggerInterceptor6 implements NestInterceptor {
    async intercept(context: ExecutionContext, next: any): Promise<Observable<any>> {
        console.log('LoggerInterceptor6 Before...');

        const now = Date.now();
        // next.handle 就是原方法
        return next.handle()
            .pipe(
                // tap在 handle执行后执行，传入一个函数该函数接收参数就是 原方法的返回值
                tap((handleValue) => {
                    console.log(`LoggerInterceptor6 After... ${Date.now() - now}ms`)
                })
            )
    }
}

@Injectable()
export class LoggerInterceptorProvider implements NestInterceptor {
    async intercept(context: ExecutionContext, next: any): Promise<Observable<any>> {
        console.log('LoggerInterceptorProvider Before...');

        const now = Date.now();
        // next.handle 就是原方法
        return next.handle()
            .pipe(
                // tap在 handle执行后执行，传入一个函数该函数接收参数就是 原方法的返回值
                tap((handleValue) => {
                    console.log(`LoggerInterceptorProvider After... ${Date.now() - now}ms`)
                })
            )
    }
}
